package com.example.campuslink.commen.adpter

import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.core.CoreAdapter
import com.example.campuslink.commen.core.CoreViewHolder
import com.example.campuslink.commen.db.school.SchoolInfoTable
import com.example.campuslink.databinding.AdapterSchoolBinding
import com.example.campuslink.viewModel.SchoolChoiceViewModel

class SchoolAdapter(vm:SchoolChoiceViewModel):  CoreAdapter<AdapterSchoolBinding, SchoolChoiceViewModel, SchoolInfoTable>(
    BR.data,
    BR.viewModel,
    vm
) {
    override fun getItemLayoutId(viewType: Int): Int {
        return R.layout.adapter_school
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CoreViewHolder {
        val holder = super.onCreateViewHolder(parent, viewType)

        if (mViewModel?.isAdmin?.value == true) {
            holder.itemView.setOnLongClickListener {
                val position = holder.bindingAdapterPosition
                Toast.makeText(
                    parent.context,
                    "删除学校：${mListData[position].schoolName}",
                    Toast.LENGTH_SHORT
                ).show()
                mViewModel.deleteSchool(mListData[position].id)
                return@setOnLongClickListener true
            }
        }
        holder.itemView.setOnClickListener {
            val position = holder.bindingAdapterPosition
            mViewModel?.addSchool(mListData[position].schoolName)
            mViewModel?.jumpToMainActivity()
        }
        return holder
    }

    override fun onBindViewHolder(holder: CoreViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        Glide.with(holder.itemView.context).load(mListData[position].schoolImage)
            .error(R.drawable.ic_empty)
            .into(holder.binding.root.findViewById<ImageView>(R.id.iv_schoolImg))
        var introduce = ""
        mListData[position].schoolIntroduce.forEach {
            introduce += it.trim()+"\t\t"
        }
        holder.binding.root.findViewById<TextView>(R.id.tv_content).text = introduce

    }
}